package cn.edu.xmu.oomall.customer.dao;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.mapper.AddressPoMapper;
import cn.edu.xmu.oomall.customer.model.bo.Address;
import cn.edu.xmu.oomall.customer.model.po.AddressPo;
import cn.edu.xmu.oomall.customer.model.po.AddressPoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.setPoModifiedFields;
import static cn.edu.xmu.privilegegateway.annotation.util.Common.setPoCreatedFields;
import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Jiawei Zheng
 * @date 2021-12-09
 */
@Repository
public class AddressDao {

    @Autowired
    AddressPoMapper addressPoMapper;

    private static Logger logger = LoggerFactory.getLogger(Common.class);

    /**
     * 买家新增地址
     * @param userId
     * @param userName
     * @param address
     * @return
     */
    public ReturnObject addAddress(Long userId, String userName, Address address){
        try{
            AddressPo po = cloneVo(address, AddressPo.class);
            po.setBeDefault((byte)0);
            setPoCreatedFields(po, userId, userName);
            addressPoMapper.insert(po);
            return new ReturnObject(cloneVo(po, Address.class));
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /**
     * 买家获取所有地址列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    public ReturnObject getAllAddresses(Long userId, Integer page, Integer pageSize){
        try{
            //PageHelper.startPage(page, 30);
            AddressPoExample example = new AddressPoExample();
            AddressPoExample.Criteria criteria = example.createCriteria();
            criteria.andCustomerIdEqualTo(userId);
            List<AddressPo> poList = addressPoMapper.selectByExample(example);
            List<Address> boList = new ArrayList<>();
            for(AddressPo po: poList){
                Address bo = cloneVo(po, Address.class);
                if(po.getBeDefault().equals((byte)1)){
                    bo.setBeDefault(true);
                }
                else{
                    bo.setBeDefault(false);
                }
                boList.add(bo);
            }
            //PageInfo pageInfo = PageInfo.of(poList);
            //pageInfo.setList(boList);
            return new ReturnObject(boList);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    public ReturnObject setDefaultAddress(Long userId, String userName, Long id){
        try{
            AddressPo addressPo = addressPoMapper.selectByPrimaryKey(id);
            if(addressPo==null){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            if(!addressPo.getCustomerId().equals(userId)){
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            //先找到原先默认地址，设置为非默认
            AddressPoExample example = new AddressPoExample();
            AddressPoExample.Criteria criteria = example.createCriteria();
            criteria.andBeDefaultEqualTo((byte)1);
            criteria.andCustomerIdEqualTo(userId);
            List<AddressPo> poList = addressPoMapper.selectByExample(example);
            if(poList.size()!=0){
                AddressPo po = poList.get(0);
                po.setBeDefault((byte)0);
                addressPoMapper.updateByPrimaryKey(po);
            }
            addressPo.setBeDefault((byte)1);
            setPoModifiedFields(addressPo, userId, userName);
            addressPoMapper.updateByPrimaryKeySelective(addressPo);
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /**
     * 买家修改地址信息
     * @param userId
     * @param userName
     * @param id
     * @param address
     * @return
     */
    public ReturnObject updateAddress(Long userId, String userName, Long id, Address address) {
        try{
            AddressPo po = addressPoMapper.selectByPrimaryKey(id);
            if(po==null){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            if(!po.getCustomerId().equals(userId)){
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            setPoModifiedFields(po, userId, userName);
            po = cloneVo(address, AddressPo.class);
            addressPoMapper.updateByPrimaryKeySelective(po);
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /**
     * 用户删除地址
     * @param id
     * @return
     */
    public ReturnObject deleteAddress(Long userId, Long id){
        try{
            AddressPo po = addressPoMapper.selectByPrimaryKey(id);
            if(po==null){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            if(!po.getCustomerId().equals(userId)){
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            addressPoMapper.deleteByPrimaryKey(id);
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }
}
